package com.minelx.sequence.pattern;

import com.minelx.sequence.core.JoinMatcher;
import com.minelx.sequence.core.Match;
import com.minelx.sequence.core.ReturnMatcher;
import com.minelx.sequence.core.StringScanner;

public class JoinPatternMatcher implements ReturnMatcher<ReturnMatcher<?, StringScanner>, StringScanner> {
	private final ReturnMatcher<?, StringScanner> previousMatcher;

	public JoinPatternMatcher(ReturnMatcher<?, StringScanner> previousMatcher) {
		this.previousMatcher = previousMatcher;
	}

	@Override
	public boolean isMatch(StringScanner clue) {
		return clue.exists("~");
	}

	@Override
	public ReturnMatcher<?, StringScanner> onMatched(StringScanner clue) {
		clue.goes("~");
		String joinerAsText = clue.til('~');
		return new JoinMatcher<>(Match.content(joinerAsText), previousMatcher);
	}
}
